<?php

namespace App\Controller\Admin;

use App\Controller\InsideService\GameUserInside;
use App\Middleware\AdminMiddleware;
use Exception;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Di\Annotation\Inject;
use Hyperf\WebSocketClient\ClientFactory;
use Hyperf\WebSocketClient\Exception\ConnectException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

#[Controller(prefix: '/admin/GameUser'), Middleware(AdminMiddleware::class)]
class GameUserController extends BaseController {
    /**
     * @Inject
     * @var ClientFactory
     */
    protected ClientFactory $clientFactory;

    /**
     * 获取平台列表
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $userDataArr = $this->uInfo();
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $page = $this->request->query('page', 1);
        $game_hash = $this->request->query('game_hash', '');
        $openid = $this->request->query('openid', '');
        $nickname = $this->request->query('nickname', '');
        $id = $this->request->query('id', '');
        $black = $this->request->query('black', -1);
        if (empty($game_hash)) {
            $game_hash = $userDataArr['user_data']['bind_games'];
        }

        return (new GameUserInside())->setCode($userDataArr['connection_code'])->index([
            'page'      => $page,
            'size'      => $limit,
            'game_hash' => $game_hash,
            'openid'    => $openid,
            'nickname'  => $nickname,
            'id'        => $id,
            'black'     => $black,
        ]);
    }

    /**
     * 封禁用户
     * @return array
     * @throws Exception
     */
    #[GetMapping(path: 'blackUser')]
    public function blackUser(): array {
        $userDataArr = $this->uInfo();
        $id = $this->request->query('id', 0);
        $game_hash = $this->request->query('game_hash', 0);
        $isDelItem = $this->request->query('isDelItem', 'no');
        $isDelEquipment = $this->request->query('isDelEquipment', 'no');
        return (new GameUserInside())->setCode($userDataArr['connection_code'])->blackUser(
            $id,
            $game_hash,
            $isDelItem,
            $isDelEquipment
        );
    }

    /**
     * 封榜用户
     * @return array
     * @throws Exception
     */
    #[GetMapping(path: 'rankBlackUser')]
    public function rankBlackUser(): array {
        $userDataArr = $this->uInfo();
        $id = $this->request->query('id', 0);
        $game_hash = $this->request->query('game_hash', 0);
        return (new GameUserInside())->setCode($userDataArr['connection_code'])->rankBlackUser(
            $id,
            $game_hash,
        );
    }

    /**
     * 实时人数
     * @return array
     */
    #[GetMapping(path: 'onlineCount')]
    public function onlineCount(): array {
        $game_hash = $this->request->query('game_hash', '');
        if (!$game_hash) {
            $game_hash = '651da2028476d3e669f892fba5014ebe';
        }
        $count = 0;
        try {
            $host = env('CHAT_HOST', '172.17.16.4') . ':' . env('CHAT_PORT', 8809) . '?User-Token=System&Game-Hash=' . $game_hash;
            $client = $this->clientFactory->create($host);
            $count = $client->push('roomUser');
        } catch (ConnectException $e) {
            //var_dump(date('Y-m-d H:i:s') . '---消息发送错误---' . $e->getMessage());
        }
        return $this->buildSuccess([
            'onlineCount' => $count,
        ]);
    }

    /**
     * 用户角色列表
     * @return array
     * @throws Exception
     */
    #[GetMapping(path: 'roleList')]
    public function roleList(): array {
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $uid = $this->request->query('uid', '');
        $page = $this->request->query('page', 1);
        $game_hash = $this->request->query('game_hash', '');
        $userDataArr = $this->uInfo();

        return (new GameUserInside())->setCode($userDataArr['connection_code'])->roleList(
            $limit, $uid, $page, $game_hash
        );
    }

}